package net.huashitong.Project.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.service.MyProjectsService;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 我的项目（业主单位的接口）
 */
@Controller
@RequestMapping(value = "/myProjects")
public class MyProjectsController extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return "/modules/myProjects/" + s;
    }

    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private SupervisionTaskService taskService;
    @Autowired
    private MyProjectsService myProjectsService;


    @RequestMapping(method = RequestMethod.GET)
    public String initPage(HttpServletRequest request, Model model) {
        model.addAttribute("leftType", "我的项目");
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        SupervisionTaskFilter taskFilter = new SupervisionTaskFilter();
        taskFilter.setUserinfo(bnpUser.getPhone());
        taskFilter.setBranchType(bnpUser.getBranchType());
        taskFilter.setRole(bnpUser.getRole());
        taskFilter.setValid("1");
        int myTaskNum = taskService.getMissinTaskCount(taskFilter);
        WebUtils.setSessionAttribute(request, "myTaskNum", myTaskNum);
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("user", bnpUser);
        model.addAttribute("nodeCount", nodeCount);
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);
        return getView("query");
    }

    /**
     * 我的项目（需要关联承办单位用户）
     * @param filter
     * @param request
     * @return
     */
    //初始化数据
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute NewProjectsFilter filter, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if("经办人".equals(bnpUser.getRole())&&"0".equals(bnpUser.getBranchType())){
            filter.setContactPhone(bnpUser.getPhone());
            filter.setOccupyOperateUserPhone(bnpUser.getPhone());
        }
        if("责任人".equals(bnpUser.getRole())&&"0".equals(bnpUser.getBranchType())){
            filter.setOwnerUnitPerson(bnpUser.getPhone());
            filter.setOccupyResUserPhone(bnpUser.getPhone());
        }
        //设置默认排序
        if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
            filter.getPaging().setSortCol("sysId");
            filter.getPaging().setSortVal("desc");
        }
        List<NewProjects> resList = newProjectsService.getProjectInfo4(filter);
        return coverJqGrid(filter, resList);
    }


    /**
     * 项目统计汇总
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statisticsSummary", method = RequestMethod.GET)
    public Message planeOpenProject(HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        Map<String,Object> result = myProjectsService.queryStatisticsSummary(bnpUser);

        return coverMessage("200", "获取成功", result);
    }
}
